#ifndef _MUNIX_RBUFF_H_
#define _MUNIX_RBUFF_H_
/*******************************************************************************
 **  Name: rbuff.h                                                            **
 **  Description:                                                             **
 **                                                                           **
 **  CDDL: Open Source Initiative (OSI) Approved License                      **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/
/* Portions Copyright [2008-2010] OpenHousewares, LLC. */

// $Id-ORIG: //depot/munix/libmunix/trunk/rbuff.h#3 $
// $Id: //depot/munix/libmunix/trunk/rbuff.h#3 $

#include <stdio.h>
#include <string.h>
#include "common.h"



#define DEFAULT_RING_BUFF_SZ	16	// power of 2!!

//! @class RingBuff
//! @brief Implements a queue or circular buffer
class RingBuff
{
public:
	RingBuff(uint8 sz);
        RingBuff(const RingBuff &);
	~RingBuff() { delete [] _buff;}

	void		Put(unsigned char) volatile;
	unsigned char	Get() volatile;
	inline uint8	Empty() const { return (_head == _tail); }
	inline uint8	Size() const { return _sz; }
	inline uint8	Length() const { return _len; }
	void		CopyBuff(char * dest) const;
	inline char *	Buff() { return (char*)_buff; }
	inline void	Clear() { memset((char*)_buff,0,DEFAULT_RING_BUFF_SZ); _head=_tail=0; }
        inline volatile uint8 Head() const { return _head; }
        inline volatile uint8 Tail() const { return _tail; }

private:
	volatile uchar *_buff;
	uint8		_sz;
	volatile uint8	_head;
	volatile uint8	_tail;
	volatile uint8	_len;
};




#endif	/* _MUNIX_RBUFF_H_ */


